<?php

namespace StarCloud\Models;

trait SortTrait
{
    public static function resetSort()
    {
        self::batchUpdate([
            'conditions' => 'sort > 0',
        ], ['sort' => 0]);
    }

    public static function updateSort($id, $index)
    {
        $record = self::findFirstById($id);
        $record && $record->update(['sort' => $index]);
    }

    public static function getSortList($limit = 30)
    {
        return self::find([
            'conditions' => 'sort > 0',
            'limit' => $limit,
            'order' => 'sort ASC',
        ])->toArray();
    }
}
